<script setup lang="ts">
import { FlashOutline } from '@vicons/ionicons5'
import EsSearch from "@/components/search/EsSearch.vue";
import type {ArticleQuery} from "@/types/query/ArticleQuery";
import {searchService} from "@/apis/front/article";
import type {ArticleInfoEs} from "@/types/entity/ArticleInfoEs";

const articleList = ref<ArticleInfoEs[]>([])

const queryParams = ref<ArticleQuery>({
  pageNum: 1,
  pageSize: 10,
  keyWord: '',
})

const loading = ref(false)
const noMore = ref(false)

const getData = async () => {
  const res = await searchService(queryParams.value)
  articleList.value = articleList.value.concat(res.data.data.records)
  if (res.data.data.records.length < queryParams.value.pageSize!) {
    noMore.value = true
  }
}
const handleLoad = async () => {
  if(noMore.value || loading.value){
    return
  }
  queryParams.value.pageNum = queryParams.value.pageNum
    ? queryParams.value.pageNum + 1
    : 1
  try {
    loading.value = true
    await getData()
  } finally {
    loading.value = false
  }
}
const onSearch = () => {
  queryParams.value.pageNum = 1
  articleList.value = []
  noMore.value = false
  getData()
}
</script>

<template>
  <div class="w-full h-full flex gap-20px overflow-auto pb-20px">
    <div class="flex-1 bg-white px-20px overflow-y-auto">
      <div class="m-20px bg-#f5f5f5 p-20px">
        <n-input-group>
          <n-input placeholder="搜索" v-model:value="queryParams.keyWord">
            <template #prefix>
              <n-icon :component="FlashOutline" />
            </template>
          </n-input>
          <n-button type="info" secondary @click="onSearch"> 搜索 </n-button>
        </n-input-group>
      </div>
      <div class="flex-1 bg-white px-24px pt-24px pb-6px overflow-y-auto">
        <n-infinite-scroll
          style="height: 100%"
          :distance="10"
          @load="handleLoad"
          :scrollbar-props="{
            trigger: 'hover',
            size: 0,
          }"
          v-if="articleList.length"
        >
          <div v-for="item in articleList" :key="item.articleId" class="item mr-6px">
            <EsSearch :Article="item" />
          </div>
          <div class="flex justify-center">
            <div v-if="loading" class="text">加载中...</div>
            <div v-if="noMore" class="text">没有更多了 🤪</div>
          </div>
        </n-infinite-scroll>
        <div
          v-else
          class="flex-1 bg-white px-24px pt-24px pb-6px overflow-y-auto"
        >
          <n-empty description="你什么也找不到">
          </n-empty>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped></style>
